package de.baumann.browser.fragments


import android.annotation.SuppressLint
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import de.baumann.browser.OdinGlideModule
import de.baumann.browser.R
import de.baumann.browser.activitys.hash.PackRecordActivity
import de.baumann.browser.activitys.hash.RedPacketDetailActivity.Companion.startPacketDetailActivity
import de.baumann.browser.adapter.PackAdapter
import de.baumann.browser.api.net.vo.Packet
import de.baumann.browser.base.BaseOdinFragment
import de.baumann.browser.iview.IPackRecordView
import de.baumann.browser.present.PackRecordPresenter
import de.baumann.browser.utils.ViewUtil.emptyView
import de.baumann.browser.utils.getUserHead
import kotlinx.android.synthetic.main.fragment_receive_pack.*

/**
 *@author:junce
 *@date:2019/3/5 14:18
 *@discription: 红包记录
 */
class PackRecordFragment : BaseOdinFragment<IPackRecordView, PackRecordPresenter>(), IPackRecordView {

    private lateinit var packAdapter: PackAdapter


    override fun getLayout(): Int {
        return R.layout.fragment_receive_pack
    }

    private var status = 0

    override fun initView(view: View) {
        status = arguments!!.getInt("status")
        rvPack.layoutManager = LinearLayoutManager(context)
        packAdapter = PackAdapter(R.layout.recycler_item_receive_pack, status)
        rvPack.adapter = packAdapter
        packAdapter.emptyView = emptyView(mContext)
        packAdapter.onItemClickListener = BaseQuickAdapter.OnItemClickListener { adapter, _, position ->
            run {
                if (status == 1) {
                    val packet = adapter.getItem(position) as Packet
                    startPacketDetailActivity(mContext, packet.packetId)
                }
            }
        }

        when (status) {
            0 -> tvPackStatus.text = "共收到红包"
            1 -> tvPackStatus.text = "共发出红包"
        }

    }

    @SuppressLint("SetTextI18n")
    override fun setTotalAmount(amount: String) {
        tvAmount.text = "$amount ODIN"
    }

    override fun setPacketList(list: List<Packet>?) {
        packAdapter.setNewData(list)
    }

    override fun initData() {
        OdinGlideModule.loadWithCropAndCircle(mContext, getUserHead(), imgPackHead, R.drawable.icon_head)
        when (status) {
            0 -> {
                getPresenter().getRecivedPacks()
            }
            1 -> {
                getPresenter().getSendPacks()
            }
        }
    }

    override fun createPresenter(): PackRecordPresenter? {
        return PackRecordPresenter()
    }

    override fun createView(): IPackRecordView? {
        return this
    }

}
